package addServlet;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.sql.SQLException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.FileUploadException;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;

import dao.impl.UserDaoImpl;
import pojo.User;

/**
 * Servlet implementation class AddServlet
 */
public class AddServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
	    boolean sex;       
		String name=request.getParameter("uname");
		double sal=Double.parseDouble(request.getParameter("sal"));
		String birth=request.getParameter("birth");
		if ("f".equals(request.getParameter("sex")) || "F".equals(request.getParameter("sex"))) {
			sex=false;
		}else if ("m".equals(request.getParameter("sex")) || "M".equals(request.getParameter("sex"))) {
			sex=true;
		}else {
			sex=true;
		}
		
		if (name!=null) {
			User user=new User();
			user.setName(name);
			user.setBirth(birth);
			user.setSal(sal);
			user.setSex(sex);
			UserDaoImpl udi=new UserDaoImpl();
			try {
				udi.addUser(user);
			} catch (SQLException e) {
				response.sendRedirect("ErrorServlet");
			}
			response.sendRedirect("ShowAllServlet");
		}else {
			response.sendRedirect("ErrorServlet");
		}
	}
	


}
